package com.prs.crm.action.oa.bookmanager;

import java.io.File;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.approval.Application;
import com.prs.crm.domain.hr.Dept;
import com.prs.crm.domain.oa.bookmanager.BookRegister;
import com.prs.crm.domain.oa.bookmanager.BookType;
import com.prs.crm.service.approval.ApplicationService;
import com.prs.crm.service.approval.ShenQingTypeService;
import com.prs.crm.service.approval.impl.ShenQingTypeServiceImpl;
import com.prs.crm.service.hr.DeptService;
import com.prs.crm.service.oa.bookmanager.BookRegisterService;
import com.prs.crm.service.oa.bookmanager.BookTypeService;

public class BookRegisterAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private BookRegister bookRegister;
	private BookRegisterService bookRegisterService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private List<BookType> bookType;
	private BookTypeService bookTypeService;
	private List<Dept> dept;
	private DeptService deptService;
	private Application application;
	private ApplicationService ApplicationService;
	
	private File upload;
	private String uploadFileName;
	public BookRegister getBookRegister() {
		return bookRegister;
	}
	public void setBookRegister(BookRegister bookRegister) {
		this.bookRegister = bookRegister;
	}
	public BookRegisterService getBookRegisterService() {
		return bookRegisterService;
	}
	public void setBookRegisterService(BookRegisterService bookRegisterService) {
		this.bookRegisterService = bookRegisterService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public List<BookType> getBookType() {
		return bookType;
	}
	public void setBookType(List<BookType> bookType) {
		this.bookType = bookType;
	}
	public BookTypeService getBookTypeService() {
		return bookTypeService;
	}
	public void setBookTypeService(BookTypeService bookTypeService) {
		this.bookTypeService = bookTypeService;
	}
	public List<Dept> getDept() {
		return dept;
	}
	public void setDept(List<Dept> dept) {
		this.dept = dept;
	}
	public DeptService getDeptService() {
		return deptService;
	}
	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}
	
	public Application getApplication() {
		return application;
	}
	public void setApplication(Application application) {
		this.application = application;
	}
	public ApplicationService getApplicationService() {
		return ApplicationService;
	}
	public void setApplicationService(ApplicationService applicationService) {
		ApplicationService = applicationService;
	}
	
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	@Action("listBookRegister")
	public String listBookRegister(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(BookRegister.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getBookRegisterService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("viewBookRegister")
	public String viewBookRegister(){
		this.listBookRegister();
		return SUCCESS;
	}
	
	@Action("selectBookRegister")
	public String selectBookRegister(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(BookRegister.class);
		criteria.add(Restrictions.gt("number", 0));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getBookRegisterService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("selectBookRegisters")
	public String selectBookRegisters(){
		this.listBookRegister();
		return SUCCESS;
	}
	@Action("editBookRegister")
	public String editBookRegister(){
		this.dept=this.deptService.getAllDept();
		this.bookType=this.getBookTypeService().getAllBookType();
		return SUCCESS;
	}
	@Clew(results = { "success", "none" })
	@Action(value = "saveBookRegister", results = {
			@Result(name = "success", type = "redirect", location = "listBookRegister"),
			@Result(name = "error", type = "chain", location = "editBookRegister"),
		 })
	public String save(){
		if (upload!=null) {
			this.bookRegister.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		if (bookRegister.getBookNo()==null|"".equals(bookRegister.getBookNo().trim())) {
			this.addActionError("编号不能为空！");
			return ERROR;
		}
		if (bookRegister.getBookName()==null|"".equals(bookRegister.getBookName().trim())) {
			this.addActionError("名称不能为空！");
			return ERROR;
		}
		this.getBookRegisterService().save(bookRegister);
		application = new Application();
		application.setType("一般");
		application.setAppName("[一般]"+"图书登记");
		application.setSqType(2);
		application.setSpr(0);
		application.setPerson(bookRegister.getRecorder());
		application.setBookRegister(bookRegister);
		application.setResign("图书登记");
		this.getApplicationService().save(application);
		return SUCCESS;
	}
	@Action(value = "removeBookRegister", results = {
			@Result(name = "success", type = "redirect", location = "listBookRegister"),
			@Result(name = "error", type = "chain", location = "listBookRegister") })
	public String removeBookRegister(){
		if (ids==null) {
			this.addActionError("请选择一行进行操作！");
			return ERROR;
		}
		this.getBookRegisterService().batchRemove(ids);
		return SUCCESS;
	}
}
